package com.wtgroup.demo.leetcode.q038_外观数列;

import org.junit.Test;

/**
 * @author 60906
 * @date 2021/5/16 21:55
 */
public class Q038 {

    @Test
    public void test1() {
        System.out.println(new Solution().countAndSay(1));
        System.out.println(new Solution().countAndSay(2));
        System.out.println(new Solution().countAndSay(3));
        System.out.println(new Solution().countAndSay(4));
        System.out.println(new Solution().countAndSay(5));
    }


    class Solution {
        public String countAndSay(int n) {
            if (n == 1) {
                return "1";
            }
            String str = countAndSay(n - 1);
            StringBuilder buff = new StringBuilder();
            char curr = 0;
            int g = 0;
            for (int i = 0; i < str.length(); i++) {
                if (curr == 0) {
                    curr = str.charAt(i);
                    g++;
                } else if (str.charAt(i)==curr) {
                    g++;
                } else if(g > 0) {
                    buff.append(g).append(curr);
                    curr = str.charAt(i);
                    g = 1;
                }
            }
            if (g > 0) {
                buff.append(g).append(curr);
            }

            return buff.toString();
        }
    }

}
